home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
Pascal
/
Book Demos in Pascal
/
KeyConfig
/
Preferences.p
< prev
Wrap
Text File
|
1995-04-04
|
4KB
|
141 lines
{Preferences file handling}
{This is a stripped-down preference file handling unit. It assumes that you always want your}
{preference file in the Preferences folder, and use resources for storing the information.}
{This file works with System 7 only!}
unit Preferences;
interface
uses
{$ifc UNDEFINED THINK_PASCAL}
Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit, TextEdit, Traps, Memory, SegLoad, {}
Scrap, ToolUtils, OSUtils, Menus, Resources, StandardFile, GestaltEqu, Files, Errors,
{$endc}
Folders;
{Open the pref file, create if needed. Return appFile and prefFile.}
{Returns true if a new prefFile was created, which indicates that we should create new resources or copy from the app.}
function SetPrefFile (prefsFileName: Str255; prefCreator, prefType: OSType; var appFile, prefFile: integer): Boolean;
{Copy a resource from one file to another. Useful when SetPrefFile returns true!}
function CopyResource (fromFile, toFile: integer; theResType: ResType; id: integer): OSErr;
implementation
{ 1) Find the Preferences folder}
{ 2) Check if the prefs file already exists. If it does, open it and exit. }
{ 3) Create the prefs file and open it.}
function SetPrefFile (prefsFileName: Str255; prefCreator, prefType: OSType; var appFile, prefFile: integer): Boolean;
var
err: OSErr;
prefsFolder: Integer;
h: Handle;
s: Str255;
dirID: Longint;
vRefNum: integer;
spec: FSSpec;
begin
appFile := CurResFile; {spara programmerts resursfilreferens}
prefFile := 0;
SetPrefFile := false;
{ 1) find the Preference folder}
err := FindFolder(kOnSystemDisk, kPreferencesFolderType, true, vRefNum, dirID);
{Make a file spec to the pref folder}
err := FSMakeFSSpec(vRefNum, dirID, prefsFileName, spec);
{ 2) Check if a prefs file exists. If it does, open it and go to 5. }
prefFile := FSpOpenResFile(spec, fsRdWrPerm);
if ResError = noErr then
begin
end
else
begin
prefFile := 0;
{ 3) Create prefs file and open it.}
{Create the file}
err := FSpCreate(spec, prefCreator, prefType, 0); {smRoman = 0}
if err = noErr then
begin
{Make a resource fork for it}
FSpCreateResFile(spec, prefCreator, prefType, 0); {smRoman = 0}
if ResError <> noErr then
begin
{Error: Couldn't create resource fork}
end
else
{Open it}
prefFile := FSpOpenResFile(spec, fsRdWrPerm);
if ResError = noErr then
SetPrefFile := true; {new pref file!}
end {Create succeeded}
else
begin
{Error: Failed to create prefsfile}
end;
end; {No prefs file existed}
end; {SetPrefFile}
{Copy a resource:}
function CopyResource (fromFile, toFile: integer; theResType: ResType; id: integer): OSErr;
var
oldResFile: integer;
res, rescopy: Handle;
wasLoaded: Boolean;
theID: integer;
theType: ResType;
theName: Str255;
procedure Barf;
begin
CopyResource := ResError;
UseResFile(oldResFile); {set back to the previous resource file}
exit(CopyResource);
end;
procedure CheckError;
begin
if ResError <> noErr then
Barf;
end; {CheckError}
begin
oldResFile := CurResFile;
UseResFile(fromFile);
CheckError;
SetResLoad(false);
res := Get1Resource(theResType, id);
{Don't CheckError before doing SetResLoad(true)!!!}
SetResLoad(true);
CheckError;
if res <> nil then
begin
wasLoaded := res^ = nil;
LoadResource(res);
CheckError;
UseResFile(toFile);
CheckError;
GetResInfo(res, theID, theType, theName);
CheckError;
rescopy := res;
if HandToHand(rescopy) <> noErr then
Barf; {i stället för DetachResource(res);}
CheckError;
AddResource(rescopy, theResType, id, theName);
CheckError;
ReleaseResource(rescopy);
if not wasLoaded then
ReleaseResource(res); {If it wasn't loaded before, it shouldn't be afterwards.}
CheckError;
CopyResource := noErr;
end
else
begin
CopyResource := resNotFound;
end;
UseResFile(oldResFile);
end; {CopyResource}
end.